<?php

namespace app\controllers\h5\my;

use app\controllers\h5\base\H5Controller;
use app\models\order\Order;
use app\models\Transfer;
use app\models\user\UserAccount;
use app\models\user\UserCommission;
use app\models\user\UserConsume;
use app\models\user\UserLevel;
use app\services\Chain;
use yii\helpers\ArrayHelper;

class AccountController extends H5Controller
{
    public function hasHelp($user_id)
    {
        $count = 0;
        $c1 = 0;
        $c2 = 0;

        $list = Chain::getChildrenLevel($user_id, [
            UserLevel::$keys['vip1'],
            UserLevel::$keys['vip2'],
            // UserLevel::$keys['vip3'],
            // UserLevel::$keys['vip4']
        ]);
        $count = count($list);
        if ($count >= 2) {
            $count = 2;
        }

        if (isset($list[0])) {
            $c1 = Chain::getChildrenLevel($list[0]['id'], [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                // UserLevel::$keys['vip3'],
                // UserLevel::$keys['vip4']
            ]);
            if (count($c1) >= 2) {
                $c1 = 1;
            } else {
                $c1 = 0;
            }
        }

        if (isset($list[1])) {
            $c2 = Chain::getChildrenLevel($list[1]['id'], [
                UserLevel::$keys['vip1'],
                UserLevel::$keys['vip2'],
                // UserLevel::$keys['vip3'],
                // UserLevel::$keys['vip4']
            ]);
            if (count($c2) >= 2) {
                $c2 = 1;
            } else {
                $c2 = 0;
            }
        }

        return [
            'count' => $count,
            'c1' => $c1,
            'c2' => $c2,
        ];

    }

    public function actionAccount()
    {
        $account = UserAccount::find()
            ->where(['user_id' => $this->user->id])
            ->asArray()
            ->one();

        // 业绩
        $children = Chain::getChildrenAll($this->user->id);
        $children = ArrayHelper::getColumn($children, 'id');
        $children = array_merge([$this->user->id], $children);

        $my = (float)Order::find()
            ->where(['user_id' => $this->user->id])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->sum('total_pay');
        $team = (float)Order::find()
            ->where(['user_id' => $children])
            ->andWhere(['!=', 'status', Order::$statusMapping['CANCEL']])
            ->andWhere(['!=', 'status', Order::$statusMapping['DELETE']])
            ->andWhere(['!=', 'status', Order::$statusMapping['NON_PAYMENT']])
            ->sum('total_pay');

        // 直推前两位进度
        $count = $this->hasHelp($this->user->id);

        return $this->successEncode([
            'account' => $account,
            'my' => $my,
            'team' => $team,
            'count' => $count
        ]);
    }

    public function actionCommission()
    {
        $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserCommission::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id]);

        switch ($type) {
            case 'score':
                $query->andWhere(['>', 'score', 0]);
                break;
            case 'ticket':
                $query->andWhere(['>', 'ticket', 0]);
                break;
            case 'platform_ticket':
                $query->andWhere(['>', 'platform_ticket', 0]);
                break;
            case 'balance':
                $query->andWhere(['>', 'balance', 0]);
                break;
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionConsume()
    {
        $type = $this->post('type');

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = UserConsume::find()
            ->where(['status' => 1])
            ->andWhere(['user_id' => $this->user->id]);

        switch ($type) {
            case 'score':
                $query->andWhere(['>', 'score', 0]);
                break;
            case 'ticket':
                $query->andWhere(['>', 'ticket', 0]);
                break;
            case 'platform_ticket':
                $query->andWhere(['>', 'platform_ticket', 0]);
                break;
            case 'balance':
                $query->andWhere(['>', 'balance', 0]);
                break;
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    public function actionTransfer()
    {
        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Transfer::find()
            ->where([
                'or',
                ['send_id' => $this->user->id],
                ['received_id' => $this->user->id],
            ]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->asArray()
            ->all();

        foreach ($list as &$item) {
            if ($item['send_id'] == $this->user->id) {
                $item['text'] = '转出';
                $item['ops'] = '-';
            } else {
                $item['text'] = '接收';
                $item['ops'] = '+';
            }
        }

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }
}